import StringUtil from "./StringUtil";


//时间工具类
export default class TimeUtil{

    /**
     * 格式化时间戳
     * @param timeStamp 时间戳
     * @param format 格式 YYYY/YY MM/M DD/D hh/h mm/m ss/s
     */
    static formatTime(timeStamp: number, format: string): string {
        let date = new Date();
        date.setTime(timeStamp);

        let result: string = format;
        if (result.match("YYYY")) {
            result = result.replace('YYYY', StringUtil.toNumberFixed(date.getFullYear(), 4));
        }
        else if (result.match("YY")) {
            result = result.replace('YY', StringUtil.toNumberFixed(date.getFullYear(), 2));
        }

        if (result.match("MM")) {
            result = result.replace('MM', StringUtil.toNumberFixed(date.getMonth() + 1, 2));
        }
        else if (result.match("M")) {
            result = result.replace('M', (date.getMonth() + 1) + "");
        }

        if (result.match("DD")) {
            result = result.replace('DD', StringUtil.toNumberFixed(date.getDate(), 2));
        }
        else if (result.match("D")) {
            result = result.replace('D', (date.getDate()) + "");
        }

        let h = date.getHours();
        let m = date.getMinutes();
        let second = date.getSeconds();

        if (result.match("hh")) {
            result = result.replace("hh", StringUtil.toNumberFixed(h, 2));
        }
        else if (result.match("h")) {
            result = result.replace("h", (h + ""));
        }

        if (result.match("mm")) {
            result = result.replace("mm", StringUtil.toNumberFixed(m, 2));
        }
        else if (result.match("m")) {
            result = result.replace("m", (m + ""));
        }

        if (result.match("ss")) {
            result = result.replace("ss", StringUtil.toNumberFixed(second, 2));
        }
        else if (result.match("s")) {
            result = result.replace("s", (second + ""));
        }
        return result;
    }

    /**
     * 格式化倒计时
     * @param second 毫秒
     * @param format 格式 hh/h mm/m ss/s
     */
    static formatSecond(second: number, format: string, usems: boolean = false): string {
        let result: string = format;
        
        let second2 = second;
        second = second / 1000;

        if (!usems) {
            second = Math.floor(second);
        }

        if (format.indexOf("d") >= 0) {
            let d: number = Math.floor(second / (3600 * 24));
            second -= d * 3600 * 24;
            if (format.indexOf("dd") >= 0) {
                result = result.replace("dd", StringUtil.toNumberFixed(d, Math.max(d.toString().length, 2)) + "D");
            }
            else {
                result = result.replace("d", d + "");
            }
        }


        if (format.indexOf("h") >= 0) {
            let h: number = Math.floor(second / 3600);
            second -= h * 3600;
            if (format.indexOf("hhh") >= 0) {
                result = result.replace("hhh", StringUtil.toNumberFixed(h, 3));
            } else if (format.indexOf("hh") >= 0) {
                result = result.replace("hh", StringUtil.toNumberFixed(h, 2));
            }
            else {
                result = result.replace("h", h + "");
            }
        }

        if (format.indexOf("m") >= 0) {
            let m: number = Math.floor(second / 60);
            second -= m * 60;

            if (format.indexOf("mm") >= 0) {
                result = result.replace("mm", StringUtil.toNumberFixed(m, 2));
            }
            else {
                result = result.replace("m", m + "");
            }
        }

        if (format.indexOf("ss") >= 0) {
            result = result.replace("ss", StringUtil.toNumberFixed(second, 2));
        }
        else {
            result = result.replace("s", second + "");
        }

        if (usems && format.indexOf("ms") >= 0) {
            let ms: number = second2;
            result = result.replace("ms", StringUtil.toNumberFixed(ms, 3));
        }

        return result;
    }

    static remainderString(end:number){
        let time = TimeUtil.remainder(end);
        let time2 = time / 1000;
        if(time2 > (60 * 60 * 24)) return TimeUtil.formatSecond(time,"d天 hh:mm:ss");
        if(time2 > (60 * 60)) return TimeUtil.formatSecond(time,"hh:mm:ss");
        if(time2 > (60)) return TimeUtil.formatSecond(time,"mm:ss");
        return TimeUtil.formatSecond(time,"s");
    }

    //返回剩余时间(s)
    static remainder(end:number){
        return Math.max(end - Date.now(),0);
    }

    /**
     * 剩余时间 格式化倒计时
     * @param end 结束时间
     * @param format 格式 hh/h mm/m ss/s
     */
    formatRemainder(end:number,format: string){
        return TimeUtil.formatSecond(TimeUtil.remainder(end),format);
    }


}
